package ac.il.technion.cs236369.hw5.core.servlets.orders;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;

import ac.il.technion.cs236369.hw5.core.entities.Instrument;
import ac.il.technion.cs236369.hw5.core.entities.OrderItem;

public enum OrderItemParameters {

	ORDER_ITEM_START_SLOT( "startSlot") {
		public void saveParameters( OrderItem orderItem, Map<String, String> parameters, EntityManager manager) {
			orderItem.setStartSlot(Integer.valueOf( parameters.get(toString( ))));
		}
	},
	ORDER_ITEM_SLOTS_COUNT( "slotsCount") {
		public void saveParameters( OrderItem orderItem, Map<String, String> parameters, EntityManager manager) {
			orderItem.setSlotsCount(Integer.valueOf( parameters.get(toString( ))));
		}
	},
	ORDER_ITEM_INSTRUMENT_ID( "instrumentId") {
		public void saveParameters( OrderItem orderItem, Map<String, String> parameters, EntityManager manager) {
			String $ = parameters.get( "insrumentId");
			if ( $ == null) {
				return;
			}
			Instrument instrument = manager.find( Instrument.class, $);
			if ( instrument == null) {
				return;
			}
			orderItem.setInstrument(instrument);
		}
	},
	UNDEFINED( "undefined") {
		public void saveParameters( OrderItem orderItem, Map<String, String> parameters, EntityManager maneger) {
			return;
		}
	};
	
	private String name;
	
	private static Map<String, OrderItemParameters> map = new HashMap<String, OrderItemParameters>( );
	
	static {
		for ( OrderItemParameters $ : OrderItemParameters.values( )) {
			map.put( $.name, $);
		}
	}
	
	private OrderItemParameters( String name) {
		this.name = name;
	}
	
	@Override
	public String toString( ) {
		return name;
	}
	
	public OrderItemParameters fromString( String name) {
		OrderItemParameters $ = map.get(name);
		if ( $ == null)
			return UNDEFINED;
		return $;
	}
	
	abstract public void saveParameters( OrderItem orderItem, Map< String, String> parameters, EntityManager manager);
}
